package com.zy.algorithm.controller;

import android.animation.Animator;
import android.animation.ObjectAnimator;
import android.view.View;
import android.view.ViewGroup;

import com.zy.algorithm.R;
import com.zy.algorithm.bean.SortStepBean;
import com.zy.algorithm.fragment.BaseAlgorithmFragment;
import com.zy.utils.AnimatorUtils;
import com.zy.zlog.ZLog;

import java.util.ArrayList;
import java.util.List;

public class IndexController {

    private static final String TAG = "IndexController";

    protected ViewGroup op_sort_index_container;
    protected ViewGroup sort_index_container;
    //下标
    protected View sort_first_index;
    protected View sort_second_index;
    protected View sort_op_first_index;
    protected View sort_op_second_index;

    protected float stepOne;

    public IndexController(View root, float stepOne) {
        this.stepOne = stepOne;
        sort_index_container = root.findViewById(R.id.sort_index_container);
        op_sort_index_container = root.findViewById(R.id.op_sort_index_container);


        sort_first_index = root.findViewById(R.id.sort_first_index);
        sort_second_index = root.findViewById(R.id.sort_second_index);
        sort_op_first_index = root.findViewById(R.id.sort_op_first_index);
        sort_op_second_index = root.findViewById(R.id.sort_op_second_index);
    }

    public void hideAllIndex() {
        hideAllIndex(false, false);
    }

    public void hideAllIndex(boolean sortIndex, boolean opSortIndex) {
        sort_index_container.setVisibility(sortIndex ? View.VISIBLE : View.GONE);
        op_sort_index_container.setVisibility(opSortIndex ? View.VISIBLE : View.GONE);
    }


    //下标动画
    public void sortIndexAnimation(SortStepBean curStepBean, BaseAlgorithmFragment.AnimationListener listener) {
        int firstIndex = curStepBean.getFirstIndex();
        int secondIndex = curStepBean.getSecondIndex();
        int opFirstIndex = curStepBean.getOpFirstIndex();
        int opSecondIndex = curStepBean.getOpSecondIndex();

        ZLog.d(TAG, "sortIndexAnimation firstIndex:" + firstIndex + " secondIndex:" + secondIndex + " opFirstIndex:" + opFirstIndex + " opSecondIndex:" + opSecondIndex);


        Animator firstIndexAni = getFirstIndexAnimator(firstIndex);
        Animator secondIndexAni = getSecondIndexAnimator(secondIndex);
        Animator minIndexAni = getMinIndexAnimator(opFirstIndex);
        Animator secondIndexAnimator = getOpSecondIndexAnimator(opSecondIndex);

        List<Animator> list = new ArrayList<>();
        list.add(firstIndexAni);
        list.add(secondIndexAni);
        list.add(minIndexAni);
        list.add(secondIndexAnimator);

        AnimatorUtils.togetherStart(list, 500, new AnimatorUtils.AnimationListener() {
            @Override
            public void onAnimationEnd() {
                super.onAnimationEnd();
                listener.onAnimationEnd();
            }
        });
    }

    public void setMinIndex(int newIndex) {
        Animator animator = getMinIndexAnimator(newIndex);
        animator.start();
    }

    public Animator getOpIndexEndAnimation(SortStepBean curStepBean) {
        int opFirstIndexEnd = curStepBean.getOpFirstIndexEnd();
        int opSecondIndexEnd = curStepBean.getOpSecondIndexEnd();
        ZLog.d(TAG, "getOpIndexAnimation opFirstIndexEnd:" + opFirstIndexEnd + " opSecondIndexEnd:" + opSecondIndexEnd);


        Animator minIndexAni = getMinIndexAnimator(opFirstIndexEnd);
        Animator secondIndexAnimator = getOpSecondIndexAnimator(opSecondIndexEnd);

        List<Animator> list = new ArrayList<>();
        list.add(minIndexAni);
        list.add(secondIndexAnimator);

        return AnimatorUtils.getTogetherStart(list);
    }


    public Animator getMinIndexAnimator(int newIndex) {
        return ObjectAnimator.ofFloat(sort_op_first_index, "translationX", sort_op_first_index.getTranslationX(), newIndex * stepOne);
    }

    public Animator getOpSecondIndexAnimator(int newIndex) {
        return ObjectAnimator.ofFloat(sort_op_second_index, "translationX", sort_op_second_index.getTranslationX(), newIndex * stepOne);
    }


    private Animator getFirstIndexAnimator(int newIndex) {
        return ObjectAnimator.ofFloat(sort_first_index, "translationX", sort_first_index.getTranslationX(), newIndex * stepOne);
    }

    private Animator getSecondIndexAnimator(int newIndex) {
        return ObjectAnimator.ofFloat(sort_second_index, "translationX", sort_second_index.getTranslationX(), newIndex * stepOne);
    }
}
